package com.ruoyi.college.controller;

import com.ruoyi.college.domain.AdminClassTrainingLink;
import com.ruoyi.college.domain.AdministrativeClass;
import com.ruoyi.college.service.IAdministrativeClassService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 行政班管理Controller
 * 
 * @author chenxl
 * @date 2025-05-19
 */
@RestController
@RequestMapping("/college/administrativeClass")
public class AdministrativeClassController extends BaseController
{
    @Autowired
    private IAdministrativeClassService administrativeClassService;

    /**
     * 查询行政班管理列表
     */
    //@PreAuthorize("@ss.hasPermi('college:administrativeClass:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdministrativeClass administrativeClass)
    {
        startPage();
        List<AdministrativeClass> list = administrativeClassService.selectAdministrativeClassList(administrativeClass);
        return getDataTable(list);
    }

    /**
     * 导出行政班管理列表
     */
    @PreAuthorize("@ss.hasPermi('college:administrativeClass:export')")
    @Log(title = "行政班管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdministrativeClass administrativeClass)
    {
        List<AdministrativeClass> list = administrativeClassService.selectAdministrativeClassList(administrativeClass);
        ExcelUtil<AdministrativeClass> util = new ExcelUtil<AdministrativeClass>(AdministrativeClass.class);
        util.exportExcel(response, list, "行政班管理数据");
    }

    /**
     * 获取行政班管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('college:administrativeClass:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(administrativeClassService.selectAdministrativeClassById(id));
    }

    /**
     * 新增行政班管理
     */
    @PreAuthorize("@ss.hasPermi('college:administrativeClass:add')")
    @Log(title = "行政班管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated AdministrativeClass administrativeClass) {
        try {
            // 生成唯一ID
            administrativeClass.setId(IdUtils.fastSimpleUUID());

            // 调用服务层，返回新增后的完整对象
            AdministrativeClass insertedClass = administrativeClassService.insertAdministrativeClass(administrativeClass);

            // 验证插入结果
            if (insertedClass != null && insertedClass.getId() != null) {
                return AjaxResult.success("新增成功", insertedClass);
            } else {
                return AjaxResult.error("新增行政班失败");
            }
        } catch (Exception e) {
            // 记录日志并返回异常信息
            System.err.println("新增行政班时发生异常: " + e.getMessage());
            return AjaxResult.error("新增行政班失败: " + e.getMessage());
        }
    }


    /**
     * 修改行政班管理
     */
    @PreAuthorize("@ss.hasPermi('college:administrativeClass:edit')")
    @Log(title = "行政班管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable String id, @RequestBody @Validated AdministrativeClass administrativeClass) {
        try {
            // 1. 确保 ID 一致（防止传错 ID）
            if (!id.equals(administrativeClass.getId())) {
                return AjaxResult.error("请求参数 ID 与行政班 ID 不匹配");
            }

            // 2. 设置更新时间为当前时间
            administrativeClass.setUpdateTime(DateUtils.getNowDate());

            // 3. 调用服务层方法，返回更新后的完整对象
            AdministrativeClass updatedClass = administrativeClassService.updateAdministrativeClass(administrativeClass);

            // 4. 验证更新结果
            if (updatedClass != null && updatedClass.getId() != null) {
                return AjaxResult.success("修改成功", updatedClass);
            } else {
                return AjaxResult.error("修改行政班失败");
            }
        } catch (Exception e) {
            // 5. 异常处理
            return AjaxResult.error("修改行政班失败: " + e.getMessage());
        }
    }

    /**
     * 删除行政班管理
     */
    @PreAuthorize("@ss.hasPermi('college:administrativeClass:remove')")
    @Log(title = "行政班管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(administrativeClassService.deleteAdministrativeClassByIds(ids));
    }
}
